package com.morningstar.qs.api.basic.util;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.mstar.qs.common.DefQsException;

/**
 * @deprecated
 * @author dchen2
 * 
 */
public class HttpClientSimpleImpl implements MsHttpClient {

	private Logger log = Logger.getLogger(HttpClientSimpleImpl.class);

	private String protocal = "http";
	private String host;
	private int port = -1;
	private String path = null;

	public HttpClientSimpleImpl() {
	}

	public HttpClientSimpleImpl(String host) {
		this(null, host, -1, null);
	}

	public HttpClientSimpleImpl(String host, String path) {
		this(null, host, -1, path);
	}

	public HttpClientSimpleImpl(String protocol, String host, int port,
			String path) {
		if (protocol != null)
			this.protocal = protocol;
		this.host = host;
		if (port > 0)
			this.port = port;
		if (path != null)
			this.path = path;
	}

	@Override
	public QueryCombination execute(LinkedHashMap<String, String> params)
			throws DefQsException {
		final HttpClient httpclient = new DefaultHttpClient();
		URI u = null;
		String responseString = null;
		try {
			u = assembleUri(this.protocal, this.host, this.port, this.path,
					params, null);
			HttpGet httpget = new HttpGet(u);
			HttpResponse response = httpclient.execute(httpget);
			responseString = EntityUtils.toString(response.getEntity());

		} catch (Exception e) {
			throw new DefQsException(
					"Failed to execute post request with url: " + u == null ? ""
							: u.toString(), e);
		}
		return new QueryCombination(u, responseString);

	}

	@Override
	public QueryCombination execute(String url) throws ClientProtocolException,
			IOException, URISyntaxException {
		final HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		HttpResponse response = httpclient.execute(httpget);
		String responseString = EntityUtils.toString(response.getEntity());
		return new QueryCombination(responseString);
	}

	@Override
	public QueryCombination executePost(String url)
			throws ClientProtocolException, IOException, URISyntaxException {
		final HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpget = new HttpPost(url);
		HttpResponse response = httpclient.execute(httpget);
		String responseString = EntityUtils.toString(response.getEntity());
		return new QueryCombination(responseString);
	}

	/*
	 * private URI assembleUri(String scheme, String host, int port, String
	 * path, Map<String, String> queryQarams, String fragment) throws
	 * URISyntaxException { URI uri = URIUtils.createURI(scheme, host, port,
	 * path, generateQuery(queryQarams), fragment); return uri; }
	 * 
	 * 
	 * private String generateQuery(Map<String, String> queryQarams) { if
	 * (queryQarams == null || queryQarams.size() == 0) return null;
	 * StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String>
	 * entry : queryQarams.entrySet()) { if (sb.length() > 0) sb.append("&");
	 * sb.append(entry.getKey()).append("=").append(entry.getValue()); } if
	 * (sb.length() > 0) return sb.toString(); return null; }
	 */

	public void setPath(String path) {
		this.path = path;
	}

	public void setProtocol(String protocol) {
		this.protocal = protocol;
	}

	public void setPort(int port) {
		this.port = port;
	}

//	public static class MainTester {
//
//		public static void main(String... arg) {
//
//			HttpClientSimpleImpl httpClient = new HttpClientSimpleImpl(
//					"veright.blog.163.com");
//			httpClient.setPath("/blog/static/28345389200911185381341");
//			LinkedHashMap<String, String> map = null;
//			try {
//				System.out.println(httpClient.execute(map));
//			} catch (ClientProtocolException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (URISyntaxException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//	}

	public static URI assembleUri(String scheme, String host, int port,
			String path, Map<String, String> queryQarams, String fragment)
			throws URISyntaxException {
		String protocol = "http";
		if (scheme != null)
			protocol = scheme;
		URI uri = URIUtils.createURI(protocol, host, port, path,
				generateQuery(queryQarams), fragment);
		return uri;
	}

	private static String generateQuery(Map<String, String> queryQarams) {
		if (queryQarams == null || queryQarams.size() == 0)
			return null;
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : queryQarams.entrySet()) {
			if (sb.length() > 0)
				sb.append("&");
			sb.append(entry.getKey()).append("=").append(entry.getValue());
		}
		if (sb.length() > 0)
			return sb.toString();
		return null;
	}

}
